package uit.nfcmenu.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetworkHelper {
	public static final String tag = NetworkHelper.class.getSimpleName();
	
	/**
	 *  Check network status
	 * @param context
	 * @return
	 */
	public static boolean checkNetworkStatus(Context context) {
		ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(
				Context.CONNECTIVITY_SERVICE);
		NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (wifi.isAvailable()) {
			if (wifi.getState() == NetworkInfo.State.CONNECTED) {
				// Log.i(tag, "Wifi is available");
				return true;
			}
		} else if (mobile.isAvailable()) {
			if (mobile.getState() == NetworkInfo.State.CONNECTED) {
				// Log.i(tag, "Mobile 3G is available");
				return true;
			}
		} 
		else {
			Log.i(tag, "No network");
		}
		return false;
	}
	
	public static void postMenu() {
		Log.i(tag, "postMenu()");
		
	    // Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost("http://restaurant-webservice.appspot.com/menu/newmenu.jsp?submenuid=1002");

	    try {
	        // Add your data
	        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
	        nameValuePairs.add(new BasicNameValuePair("description", "Cool!"));
	        nameValuePairs.add(new BasicNameValuePair("menuName", "AndDev"));
	        nameValuePairs.add(new BasicNameValuePair("subMenuID", "0"));
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

	        // Execute HTTP Post Request
	        HttpResponse response = httpclient.execute(httppost);
	        
	    } catch (ClientProtocolException e) {
	        // TODO Auto-generated catch block
	    	e.printStackTrace();
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	    	e.printStackTrace();
	    }
	} 
}
